import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _slice from "lodash/slice";
import _isUndefined from "lodash/isUndefined";
import _forEach from "lodash/forEach";
import _cloneDeep from "lodash/cloneDeep";
import { getNewUUIDKey, noop, commonParseForField } from "./common";
import DataType from "../DataType";
import { convertParseTemplates, convertExtraTemplates } from '.';
/**
 * templateType 为 Tabs 的 tpl 添加进 设计器 需要做的格式转化操作
 * @param {object} template
 * @param {object} options
 * @param {boolean} options.noCloneDeep 不需要将 template 复制一份(即不改变原数据)
 * @returns {object} 经过转化后的 tpl
 */

function dynamicTabsInitExtra(template, options) {
  var noCloneDeep = options && options.noCloneDeep;
  var newTemplate = noCloneDeep ? template : _cloneDeep(template);
  return newTemplate;
}
/**
 * 当向 templateType 为 Tabs 的 tpl 添加 field 时 需要做的格式转化操作
 * @param {object} template - 设计器中的 tpl 数据
 * @param {object} field - 添加进 tpl 的 field
 * @param {object} options
 * @param {boolean} options.noCloneDeep - 不需要将 field 复制一份(即不改变原数据)
 * @returns {object} 经过转化后的 field
 */


function dynamicTabsInitExtraField(template, field, options) {
  var noCloneDeep = options && options.noCloneDeep;
  var newField = noCloneDeep ? field : _cloneDeep(field); // newField.fieldLabel

  newField.fieldName = getNewUUIDKey();
  return newField;
}
/**
 * 从服务器中拿到的 templateType 为 Tabs 的tpl 转化成设计器的 格式转化
 * @param {object} template - 服务器中 存储的 tpl
 * @param {object} options
 * @param {boolean} options.noCloneDeep - 不需要将 template 复制一份(即不改变原数据)
 * @returns {object} 设计器需要的 tpl
 */


function dynamicTabsDirtyExtra(template, options) {
  var noCloneDeep = options && options.noCloneDeep;
  var newTemplate = noCloneDeep ? template : _cloneDeep(template);
  var _newTemplate$fields = newTemplate.fields,
      fields = _newTemplate$fields === void 0 ? [] : _newTemplate$fields,
      _newTemplate$children = newTemplate.children,
      children = _newTemplate$children === void 0 ? [] : _newTemplate$children;

  _forEach(fields, function (field) {
    var extraField = dynamicTabs.dirtyExtraFields[field.componentType] || noop;
    extraField(newTemplate, field);

    var tabsTemplates = _slice(children, field.tplFrom, field.tplTo); // eslint-disable-next-line no-param-reassign


    field.children = convertExtraTemplates(tabsTemplates);
    return field;
  });

  delete newTemplate.children;
  return newTemplate;
}
/**
 * 从设计器中的数据 转化成 服务器需要的提交数据
 * @param {object} template - 需要转化的tpl
 * @param {?object} options
 * @param {?boolean} options.noCloneDeep - 不需要将 template 复制一份(即不改变原数据)
 * @returns {object} 服务器提交需要的 tpl
 */


function dynamicTabsDirtyParse(template, options) {
  var noCloneDeep = options && options.noCloneDeep;
  var newTemplate = noCloneDeep ? template : _cloneDeep(template);
  var newChildren = [];
  var orderSeq = 0;
  var orderSeqInc = 10;
  var tplFrom = -1;
  var tplTo = -1;

  _forEach(newTemplate.fields, function (field) {
    orderSeq += orderSeqInc; // eslint-disable-next-line no-param-reassign

    field.orderSeq = orderSeq;
    var children = field.children || [];

    if (children.length > 0) {
      tplFrom = tplTo === -1 ? 0 : tplTo;
      tplTo = tplFrom + children.length; // 将 所有的 children 打平 放到 tabs 的 children 上

      newChildren.push.apply(newChildren, _toConsumableArray(convertParseTemplates(children))); // eslint-disable-next-line no-param-reassign

      field.tplFrom = tplFrom; // eslint-disable-next-line no-param-reassign

      field.tplTo = tplTo;
    } // eslint-disable-next-line no-param-reassign


    delete field.children;
    commonParseForField(newTemplate, field);
    var parseField = dynamicTabs.dirtyParseFields[field.componentType] || noop;
    parseField(newTemplate, field);
  });

  newTemplate.children = newChildren;
  return newTemplate;
}
/**
 * 将 componentType 为 TabPane 的 额外数据 转化成 设计器数据
 * @param {object} template - 需要转化的field 所属的 template
 * @param {object} field - 需要转化的field
 * @param {?object} options
 */
// eslint-disable-next-line no-unused-vars


function dynamicTabsDirtyExtraFieldTabPane(template, field, options) {
  var tplFrom;
  var tplTo;
  var newConfig = [];

  _forEach(field.config, function (cnf) {
    switch (cnf.attributeName) {
      case 'tplFrom':
        tplFrom = cnf.value;
        break;

      case 'tplTo':
        tplTo = cnf.value;
        break;

      default:
        newConfig.push(cnf);
        break;
    }
  }); // eslint-disable-next-line no-param-reassign


  field.config = newConfig; // eslint-disable-next-line no-param-reassign

  field.tplFrom = _isUndefined(tplFrom) ? -1 : tplFrom; // eslint-disable-next-line no-param-reassign

  field.tplTo = _isUndefined(tplTo) ? -1 : tplTo;
}
/**
 * 将从服务器取得的数据 Tabs 的 field componentType 为 TabPane 的 field 转化成 设计器数据
 * @param {object} template- 需要转化的field 所属的 template
 * @param {object} field - 需要转化的field
 * @param {?object} options
 */
// eslint-disable-next-line no-unused-vars


function dynamicTabsDirtyParseFieldTabPane(template, field, options) {
  var _field$config = field.config,
      newConfig = _field$config === void 0 ? [] : _field$config,
      _field$tplFrom = field.tplFrom,
      tplFrom = _field$tplFrom === void 0 ? -1 : _field$tplFrom,
      _field$tplTo = field.tplTo,
      tplTo = _field$tplTo === void 0 ? -1 : _field$tplTo;
  newConfig.push({
    attributeName: 'tplFrom',
    attributeType: DataType.Number,
    value: tplFrom
  });
  newConfig.push({
    attributeName: 'tplTo',
    attributeType: DataType.Number,
    value: tplTo
  }); // eslint-disable-next-line no-param-reassign

  field.config = newConfig; // eslint-disable-next-line no-param-reassign

  delete field.tplFrom; // eslint-disable-next-line no-param-reassign

  delete field.tplTo;
}

var dynamicTabs = {
  initExtra: dynamicTabsInitExtra,
  initExtraField: dynamicTabsInitExtraField,
  dirtyExtra: dynamicTabsDirtyExtra,
  dirtyParse: dynamicTabsDirtyParse,
  dirtyExtraFields: {
    TabPane: dynamicTabsDirtyExtraFieldTabPane
  },
  dirtyParseFields: {
    TabPane: dynamicTabsDirtyParseFieldTabPane
  }
};
export default dynamicTabs;